<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // promise:是一种异步解决方案，解决传统回调地狱问题。使得异步操作起来更加强大合理

        //用promise封装元素ajax
        function myAjax(option) {
            return new Promise(function (resolve, reject) {
                //异步操作
                let xhr = new XMLHttpRequest();
                xhr.open(option.method, option.url);
                xhr.onload = function () {
                    if (xhr.status == 200) {
                        let res = JSON.parse(xhr.responseText);
                        resolve(res);
                    } else {
                        reject("请求失败" + xhr.status)
                    }
                }
                xhr.send();
            })
        }

        //调用封装
        myAjax({
            method: "get",
            url: "http://123.57.109.30:3006/api/getbooks",
        }).then(res => {
            console.log(res);
        })
    </script>
</body>

</html>